#include "process.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>

/**
 * @brief enumarate of operation flag
 * 
 */
enum FLAG
{
    PRINT_TO_SCREEN = 0b001,
    RPINT_TO_FILE = 0b010,
    RANDOMIZE_DESTINATION = 0b100
};

void process(Exploration *p_Exploration)
{
    int size_of_starSystemList = len_of_StarSystemList(p_Exploration->starSystemList);
    //  iterate through starship list
    for (StarshipListNode *p = p_Exploration->starshipList; p != NULL; p = p->next)
    {
        // whether random destination
        if (p->starship.ProcessingOptions & RANDOMIZE_DESTINATION)
        {

            // assign a value to a random number seed
            srand((unsigned int)time(NULL));
            // generate a random number less than the size of starSystemList
            int randVal = rand() % size_of_starSystemList + 1;
            // get a random StarSystemListNode among all starSystemList
            StarSystemListNode *rand_starship = getNumberOfSystemList(p_Exploration->starSystemList, randVal);
            // copy attribute
            strcpy(p->starship.DestinationName, rand_starship->starSystem.Name);
            p->starship.Destination = rand_starship->starSystem;
        }

        // whether save to file.
        if (p->starship.ProcessingOptions & RPINT_TO_FILE)
        {
            // output： p->starship.DestinationName
            FILE *process_file;
            process_file = fopen("results.txt", "w");
            if (process_file == NULL)
            {
                printf("process_file cannot open!");
                exit(0);
            }
            // export to result file
            fprintf(process_file, "The ship \"%s\" is going to \"%s\" star system.\n", p->starship.Name, p->starship.DestinationName);
            fclose(process_file);
        }
        // whether print to screen
        if (p->starship.ProcessingOptions & PRINT_TO_SCREEN)
        {
            printf("The ship \"%s\" is going to \"%s\" star system.\n", p->starship.Name, p->starship.DestinationName);
        }
    }
}
